<template>
  <j-modal
    title="设备控制"
    :width="800"
    :visible="visible"
    switchFullscreen
    @cancel="onCancel"
    cancelText="关闭"
    :footer="footer"
  >
    <a-form-model ref="form" :model="model" :rules="rules">
      <a-row>
        <a-col :span="24">
          <a-form-model-item label="屏幕开关" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="screenonoffstatus">
            <a-switch
              v-model="model.screenonoffstatus"
              checked-children="开"
              un-checked-children="关"
              @change="handleScreenonoffstatusChange"
            />
          </a-form-model-item>
          <a-form-model-item label="音量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="volume">
            <a-row>
              <a-col :span="14">
                <a-slider v-model="model.volume" :min="0" :max="100" :step="1" />
              </a-col>
              <a-col :span="7">
                <a-input-number v-model="model.volume" :min="0" :max="100" :step="1" style="margin-left: 16px" />
              </a-col>
              <a-col :span="3">
                <a-button type="primary" @click="saveVolume">保存</a-button>
              </a-col>
            </a-row>
          </a-form-model-item>
          <a-form-model-item label="亮度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bri">
            <a-row>
              <a-col :span="14">
                <a-slider v-model="model.bri" :min="1" :max="255" :step="1" />
              </a-col>
              <a-col :span="7">
                <a-input-number v-model="model.bri" :min="1" :max="255" :step="1" style="margin-left: 16px" />
              </a-col>
              <a-col :span="3">
                <a-button type="primary" @click="saveBri">保存</a-button>
              </a-col>
            </a-row>
          </a-form-model-item>
          <a-form-model-item label="屏幕锁定" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="screenlockedstatus">
            <a-switch
              v-model="model.screenlockedstatus"
              checked-children="锁定"
              un-checked-children="未锁定"
              @change="handleScreenlockedstatusChange"
            />
          </a-form-model-item>
          <a-form-model-item label="设备重启" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reset">
            <a-button @click="handleRestart" type="primary" ghost>点击重启</a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-modal>
</template>

<script>
import { getAction } from '@/api/manage'
const submitType = {
  screenonoffstatus: 1,
  volume: 2,
  bri: 3,
  screenlockedstatus: 4,
  reset: 5,
}
export default {
  name: 'DeviceControlModal',
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      url: '/corp/led/control',
      model: {},
      rules: {
        volume: [{ required: true, message: '请输入音量大小' }],
        bri: [{ required: true, message: '请输入亮度大小' }],
      },
    }
  },
  methods: {
    init(record) {
      this.visible = true
      this.model = Object.assign(
        {},
        {
          ...record,
          screenonoffstatus: 1 == record.screenonoffstatus ? true : false,
          screenlockedstatus: 1 == record.screenlockedstatus ? true : false,
        }
      )
    },
    footer() {
      return <a-button onClick={this.onCancel}>关闭</a-button>
    },
    onCancel() {
      this.visible = false
      this.$emit('ok')
    },
    doApi(type, value) {
      getAction(this.url, { id: this.model.id, type, value }).then((res) => {
        if (res.success) this.$message.success('操作成功！')
      })
    },
    // 屏幕开关操作
    handleScreenonoffstatusChange() {
      this.doApi(submitType.screenonoffstatus, this.model.screenonoffstatus ? 1 : 0) //1开 0关
    },
    // 屏幕锁定操作
    handleScreenlockedstatusChange() {
      this.doApi(submitType.screenlockedstatus, this.model.screenlockedstatus ? 1 : 0) //1锁定 0未锁定
    },
    // 亮度操作
    saveBri() {
      this.doApi(submitType.bri, this.model.bri)
    },
    // 音量操作
    saveVolume() {
      this.doApi(submitType.volume, this.model.volume)
    },
    // 重启
    handleRestart() {
      this.doApi(submitType.reset, 0)
    },
  },
}
</script>

<style lang="less" scoped>
</style>